﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using Timed;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using Iso;
using Iso.Common;
using Iso.Core;

namespace Game.Plugin
{
    public class GridDraw : ICustomPlugin
    {
        int size;
        GPUProgrammingServices gpu;
        MaterialType gridMaterialType;
        TriangleSelector sel;
        MeshSceneNode node;

        public GridDraw(object s)
        {
            size = (int)(double)s;
        }

        public void Load()
        {
            gpu = Engine.device.VideoDriver.GPUProgrammingServices;
            gpu.OnSetConstants += gpu_OnSetConstants;
            gridMaterialType = gpu.AddHighLevelShaderMaterialFromFiles(Engine.baseData + "shaders/grid.hlsl", "vertexMain", VertexShaderType.VS_2_0,
                Engine.baseData + "shaders/grid.hlsl", "pixelMain", PixelShaderType.PS_2_0, MaterialType.TransparentAlphaChannel);

            float s = ((Scene)Engine.currentScene).basePlane.Scale.X * 100f;
            Mesh mesh = Engine.device.SceneManager.AddHillPlaneMesh("battleGrid", new Dimension2Df(size, size), new Dimension2Di((int)(s / size), (int)(s / size)),
                Material.Identity, 0, new Dimension2Df(0, 0), new Dimension2Df(s / size, s / size));
            node = Engine.device.SceneManager.AddMeshSceneNode(mesh);
            node.SetMaterialType(gridMaterialType);
            node.SetMaterialTexture(0, Engine.device.VideoDriver.GetTexture(Engine.baseData + "spritesheets/battleui/grid.png"));
            node.Position = new Vector3Df(0, 0.1f, 0);
            sel = Engine.device.SceneManager.CreateTriangleSelector(mesh, node);
        }

        void gpu_OnSetConstants(MaterialRendererServices services, int userData)
        {
            Vector3Df collisionPoint;
            Triangle3Df collisionTriangle;
            SceneNode collisionNode;
            Engine.device.SceneManager.SceneCollisionManager.GetCollisionPoint(
                Engine.device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(LuaHook.GetMousePos()), sel,
                out collisionPoint, out collisionTriangle, out collisionNode);

            if (collisionPoint == null)
                collisionPoint = new Vector3Df(999, 0, 999);
            collisionPoint += new Vector3Df(0, 1, 0);

            Matrix worldViewProj = Engine.device.VideoDriver.GetTransform(TransformationState.Projection);
            worldViewProj *= Engine.device.VideoDriver.GetTransform(TransformationState.View);
            worldViewProj *= Engine.device.VideoDriver.GetTransform(TransformationState.World);

            services.SetVertexShaderConstant(services.GetVertexShaderConstantID("xViewProjection"), worldViewProj.ToArray());
            services.SetPixelShaderConstant(services.GetPixelShaderConstantID("xMousePos"), collisionPoint.ToArray());
        }

        public void Unload()
        {
            node.Remove();
        }

        public void OnEvent(Event evnt)
        {

        }

        public void OnUpdate()
        {

        }

        public void OnDraw()
        {

        }

        public PluginDrawOrder GetDrawOrder()
        {
            return PluginDrawOrder.Off;
        }
    }
}
